﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using LogWare.EventArgs;

namespace LogWare.AppCode {
	public class NotifiableCollection<T> : ObservableCollection<T> where T : class, INotifyPropertyChanged {
		public event EventHandler<NotifyCollectionChangeEventArgs> ItemChanged;

		protected override void ClearItems() {
			foreach (var item in this.Items) {
				item.PropertyChanged -= ItemPropertyChanged;
			}
			base.ClearItems();
		}

		protected override void SetItem(int index, T item) {
			this.Items[index].PropertyChanged -= ItemPropertyChanged;
			base.SetItem(index, item);
			this.Items[index].PropertyChanged += ItemPropertyChanged;
		}

		protected override void RemoveItem(int index) {
			this.Items[index].PropertyChanged -= ItemPropertyChanged;
			base.RemoveItem(index);
		}

		protected override void InsertItem(int index, T item) {
			base.InsertItem(index, item);
			item.PropertyChanged += ItemPropertyChanged;
		}

		private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e) {
			T changedItem = sender as T;
			OnItemChanged(this.IndexOf(changedItem), e.PropertyName);
		}

		private void OnItemChanged(int index, string propertyName) {
			if (ItemChanged != null) {
				this.ItemChanged(this, new NotifyCollectionChangeEventArgs(index, propertyName));

			}
		}
	}
}
